﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

public partial class Main_Master_Location : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string WHSID = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["WHSID"] != null)
        {
            WHSID = Request.QueryString["WHSID"].ToString();
        }
        if (Request.QueryString["action"] != null)
        {
            if (Request.QueryString["action"].ToString() == "addnew")
            {
                PnlAddNew.Visible = true;
                BtnUpdate.Visible = false;

            }
            else if (Request.QueryString["action"].ToString() == "view")
            {
                PnlView.Visible = true;
                if (!IsPostBack)
                {
                    BindGridLocation();
                }
            }
            if (!IsPostBack)
            {
                BindDDlParent();
                bindcountry();  //Bind Country dropdown
                bindstate();
            }
        }
    }
    protected void BindDDlParent()
    {
        string sql = "select WHSLOCATION, WHSID from IVR_WAREHOUSE";
        DataTable dt = DU.getdata(sql);
        DDLParentLocation.DataSource = dt;
        DDLParentLocation.DataTextField = "WHSLOCATION";
        DDLParentLocation.DataValueField = "WHSID";
        DDLParentLocation.DataBind();
        DDLParentLocation.Items.Insert(0, new ListItem("-Select Item-", "0"));
     }
    private void bindcountry()
    {
        DataTable dt = DU.getdata("select CountryId,CountryName from MAS_Country");
        DDLCountry.DataSource = dt;
        DDLCountry.DataTextField = "CountryName";
        DDLCountry.DataValueField = "CountryId";
        DDLCountry.DataBind();

    }
    private void bindstate()
    {
        DataTable DT = DU.getdata("select StateId,StateName from MAS_State");
        DDLState.DataSource = DT;
        DDLState.DataTextField = "StateName";
        DDLState.DataValueField = "StateId";
        DDLState.DataBind();

    }
    protected void BindGridLocation()
    {
        //string sql = "SELECT i1.WHSID, i1.WHSLOCATION, i2.WHSLOCATION AS parentlocation,i1.whscity, i1.WHSACTIVE FROM IVR_WAREHOUSE AS i1 LEFT OUTER JOIN IVR_WAREHOUSE AS i2 ON i1.PARENT_WHSID = i2.WHSID";
        string sql = "select i1.WHSID, i1.WHSLOCATION, i2.WHSLOCATION AS parentlocation,isnull(i1.WHSADD1,' ')+' '+isnull(i1.WHSADD2,' ')+' '+isnull(i1.WHSADD3,' ')+' '+isnull(i1.WHSADD4,' ')+' '+isnull(i1.WHSCITY,' ')+' '+isnull((select statename from MAS_State where stateid=i1.WHSSTATE),' ')+' '+isnull((Select countryname from MAS_COUNTRY where countryid=i1.WHSCOUNTRY),' ')as whscity, i1.WHSACTIVE FROM IVR_WAREHOUSE AS i1 LEFT OUTER JOIN IVR_WAREHOUSE AS i2 ON i1.PARENT_WHSID = i2.WHSID";
        DataTable dt = DU.getdata(sql);
        GrdLocation.DataSource = dt;
        GrdLocation.DataBind();
        custPager.TotalPages = 2; 
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        try
        {
            int active = 0;
            if (ChkActive.Checked == true)
            {
                active = 1;
            }
            SqlParameter[] param = {
                                 new SqlParameter("@WHSLOCATION", TxtDescription.Text.Trim()),
                                 new SqlParameter("@PARENT_WHSID",DDLParentLocation.SelectedValue),
                                 new SqlParameter("@WHSACTIVE",ChkActive.Checked),
                                 new SqlParameter("@WHSADD1",TxtAddress1.Text.Trim()),
                                 new SqlParameter("@WHSADD2",TxtAddress2.Text.Trim()),
                                 new SqlParameter("@WHSADD3",TxtAddress3.Text.Trim()),
                                 new SqlParameter("@WHSADD4",TxtAddress4.Text.Trim()),
                                 new SqlParameter("@WHSCITY",TxtCity.Text.Trim()),
                                 new SqlParameter("@WHSSTATE",DDLState.SelectedValue),
                                 new SqlParameter("@WHSCOUNTRY",DDLCountry.SelectedValue),
                                 new SqlParameter("@WHSPIN",TxtPin.Text.Trim()),
                                 new SqlParameter("@WHSPHONE",TxtPhone.Text.Trim()),
                                 new SqlParameter("@WHSFAX", TxtFax.Text.Trim()),
                                 new SqlParameter("@WHSEMAIL",TxtEmail.Text.Trim()),
                                 new SqlParameter("@WHSREMARKS", TxtRemarks.Text.Trim()),
                                 new SqlParameter("@CName",Session["username"].ToString()),
                                 new SqlParameter("@COMPANYID","1")
                                };
            DU.executeprocedure("usp_insert_IVR_WAREHOUSE", param);
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "true");
            lblMsg.Text = "Location created successfully...";
            TxtRemarks.Text = "";
            TxtDescription.Text = "";
            //BindGridLocation();

        }

        catch (Exception ex)
        {
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = ex.Message;
        }
   
          
    }
    protected void GrdLocation_SelectedIndexChanged(object sender,EventArgs e)
    {
        string WHSID = GrdLocation.SelectedDataKey.Value.ToString();
       // Response.Redirect("Location.aspx?WHSID=" + WHSID);
        PnlAddNew.Visible = true;
        PnlView.Visible = false;
        bindedit(WHSID);
    }
    private void bindedit(string key)
    {
        BindDDlParent();
        BtnSave.Visible = false;
        BtnUpdate.Visible = true;
        string sql = "SELECT * FROM IVR_WAREHOUSE where WHSID =" + key;
        DataTable dt = DU.getdata(sql);
        TxtDescription.Text = dt.Rows[0]["WHSLOCATION"].ToString();
        TxtAddress1.Text = dt.Rows[0]["WHSADD1"].ToString();
        TxtAddress2.Text = dt.Rows[0]["WHSADD2"].ToString();
        TxtAddress3.Text = dt.Rows[0]["WHSADD3"].ToString();
        TxtAddress4.Text = dt.Rows[0]["WHSADD4"].ToString();

        TxtCity.Text = dt.Rows[0]["WHSCITY"].ToString();
        if (dt.Rows[0]["WHSSTATE"].ToString() != string.Empty)
        {
            DDLState.SelectedValue = dt.Rows[0]["WHSSTATE"].ToString();
        }
        if (dt.Rows[0]["WHSCOUNTRY"].ToString() != string.Empty)
        {
            DDLCountry.SelectedValue = dt.Rows[0]["WHSCOUNTRY"].ToString();
        }
        TxtPin.Text = dt.Rows[0]["WHSPIN"].ToString();
        TxtPhone.Text = dt.Rows[0]["WHSPHONE"].ToString();
        TxtFax.Text = dt.Rows[0]["WHSFAX"].ToString();
        TxtEmail.Text = dt.Rows[0]["WHSEMAIL"].ToString();
        HidControl.Value = key;
        TxtRemarks.Text = dt.Rows[0]["WHSREMARKS"].ToString();
        if (dt.Rows[0]["WHSACTIVE"].ToString() == "True")
        {
            ChkActive.Checked = true;
        }
        else
        {
            ChkActive.Checked = false;
        }
        if (!dt.Rows[0]["PARENT_WHSID"].ToString().Equals(string.Empty) && !dt.Rows[0]["PARENT_WHSID"].ToString().Equals("0"))
        {
            DDLParentLocation.Enabled = true;
            DDLParentLocation.SelectedValue = dt.Rows[0]["PARENT_WHSID"].ToString();
        }
        else
        {
            RequiredFieldValidator1.Enabled = false;
            DDLParentLocation.Enabled = false;
        }
    }
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        string WHSID = HidControl.Value;
        try
        {
            int active = 0;
            if (ChkActive.Checked == true)
            {
                active = 1;
            }
            SqlParameter[] param = {
                                 new SqlParameter("@WHSID", WHSID),
                                 new SqlParameter("@WHSLOCATION", TxtDescription.Text.Trim()),
                                 new SqlParameter("@PARENT_WHSID",DDLParentLocation.SelectedValue),
                                 new SqlParameter("@WHSACTIVE",ChkActive.Checked),
                                 new SqlParameter("@WHSADD1",TxtAddress1.Text.Trim()),
                                 new SqlParameter("@WHSADD2",TxtAddress2.Text.Trim()),
                                 new SqlParameter("@WHSADD3",TxtAddress3.Text.Trim()),
                                 new SqlParameter("@WHSADD4",TxtAddress4.Text.Trim()),
                                 new SqlParameter("@WHSCITY",TxtCity.Text.Trim()),
                                 new SqlParameter("@WHSSTATE",DDLState.SelectedValue),
                                 new SqlParameter("@WHSCOUNTRY",DDLCountry.SelectedValue),
                                 new SqlParameter("@WHSPIN",TxtPin.Text.Trim()),
                                 new SqlParameter("@WHSPHONE",TxtPhone.Text.Trim()),
                                 new SqlParameter("@WHSFAX", TxtFax.Text.Trim()),
                                 new SqlParameter("@WHSEMAIL",TxtEmail.Text.Trim()),
                                 new SqlParameter("@WHSREMARKS", TxtRemarks.Text.Trim()),
                                 new SqlParameter("@ename",Session["username"].ToString()),
                                 new SqlParameter("@COMPANYID","1")
                                };
            DU.executeprocedure("usp_update_IVR_WAREHOUSE", param);
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "true");
            lblMsg.Text = "Location details updated successfully...";
            TxtRemarks.Text = "";
            TxtDescription.Text = "";
            PnlAddNew.Visible = false;
            BindGridLocation();

        }

        catch (Exception ex)
        {
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = ex.Message;
        }
   
    }
    protected void custPager_PageChanged(object sender, CustomPageChangeArgs e)
    {
        GrdLocation.PageSize = e.CurrentPageSize;
        GrdLocation.PageIndex = e.CurrentPageNumber;
        BindGridLocation();
    }  
    protected void BtnClose_Click(object sender, EventArgs e)
    {
        Response.Redirect("Location.aspx?action=view");  
    }
    protected void GrdLocation_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GrdLocation.PageIndex = e.NewPageIndex;
        BindGridLocation();
    }
    protected void GrdLocation_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Pager)
        {
            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridview_alternate'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
            else if (e.Row.RowState == DataControlRowState.Normal)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridviewitem'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
        }
    }
}